package com.hb.game.pk.ui.activity.mine.info;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.RefreshUserInfoEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangeNameActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_ok)
    TextView tvOk;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_name;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChangeNameActivity.this.finish();
            }
        });
    }

    String s;

    @OnClick(R.id.tv_ok)
    public void onViewClicked() {
        s = etCode.getText().toString();
        if (StringUtils.isEmpty(s)) {
            ToastUtils.showToast("请输入昵称");
            return;
        }

        call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).user_name(s);
        call.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }
                RefreshUserInfoEvent.name=s;
                EventBus.getDefault().post(new RefreshUserInfoEvent());
                APP.userInfo.setNickname(s);
                ChangeNameActivity.this.finish();
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }
}
